﻿var action = "";
var loaifont = { MLC: 0, TL: '', U: '', HT: 0 };
$(document).ready(function () {
    $("#edit-content").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 400,
        buttons: [{
            html: "<span class='glyphicon glyphicon-floppy-disk'></span> Lưu lại", class: "btn btn-default", click: function () {
                if (!checkValidate()) return false;
                loaifont.TL = $("#txtTenLoai").val();
                loaifont.U = $("#txtUrl").val();
                if ($("#HienThi1").is(':checked')) {
                    loaifont.HT = 1;
                } else {
                    loaifont.HT = 0;
                }

                if (action == 'insert') {
                    ThemMoi();
                } else {
                    CapNhat();
                }
            }
        },
            { html: "<span class='glyphicon glyphicon-remove'></span> đóng", class: "btn btn-danger", click: function () {
                $(this).dialog("close");
            }
            }
        ]
    });

    $("#ftimkiem").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 400,
        buttons: [
           { html: "<span class='glyphicon glyphicon-search'></span> Tìm", class: "btn btn-primary", click: function () {
               var ten = $("#sTenLoai").val();
               $("#stxtTenLoai").val(ten);
               console.log(ten);
               refresh();
               $("#ftimkiem").dialog("close");
           }
           },
           { html: "<span class='glyphicon glyphicon-refresh'></span> Làm mới", class: "btn btn-danger", click: function () {
               $("#sTenLoai").val("");
               $("#stxtTenLoai").val("");
           }
           }
        ]
    });

    $("#timkiem").click(function () {
        $("#ftimkiem").dialog("open");
        return false;
    });

    $("#fthem").click(function () {
        action = "insert";
        $("#edit-content").dialog("open");
        return false;
    });
});

function edit(id) {
    resetdata();
    $.post("../ajax/loaifont.aspx", { Methods: "GetInfo", Loai: id }, function (data) {
        data = $.parseJSON(data);
        action = "update";
        loaifont.MLC = data.MLC;
        $("#txtTenLoai").val(data.TL);
        $("#txtUrl").val(data.U);
        $("#HienThi"+ data.HT).prop('checked', true);

        $("#edit-content").dialog({ title: "Cập nhật" });
        $("#edit-content").dialog("open");
    });
    return false;
}

function showAlert(id) {
    jConfirm("Xác nhận xóa?", "Thông báo", function (iOK) {
        if (iOK) {
            Xoa(id);
        }
    });
    return false;
}

function Xoa(id) {
    $.post("../ajax/loaifont.aspx", { Methods: "delete", font: id }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'OK') {
            refresh();
            $("#edit-content").dialog("close");
        } else {
            jAlert(data.Mess, "Thông báo");
        }
    });
}

function ThemMoi() {
    $.post("../ajax/loaifont.aspx", { Methods: "ThemMoi", ten: loaifont.TL, path: loaifont.U, hienthi: loaifont.HT }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'Success') {
            refresh();
            $("#edit-content").dialog("close");
        } else {
            jAlert(data.Mess, "Thông báo");
        }
    });
    return false;
}

function CapNhat() {
    $.post("../ajax/loaifont.aspx", { Methods: "CapNhat", info: JSON.stringify(loaifont) }, function (data) {
        data = $.parseJSON(data);
        if (data.Status == 'Success') {
            refresh();
            $("#edit-content").dialog("close");
        } else {
            jAlert(data.Mess, "Thông báo");
        }
    });
    return false;
}

function resetdata() {
    loaifont.MLC = 0;
    $("#txtTenLoai").val("");
    $("#txtUrl").val("");
}

function checkValidate() {
    //  if($("#txtMaFont").val("");
    if ($("#txtTenLoai").val() == "") {
        jAlert("Chưa nhập tên loại!", "Thông báo");
        return false;
    }
    if ($("#txtUrl").val() == "") {
        jAlert("Chưa nhập url!", "Thông báo");
        return false;
    }
    return true;
}

function refresh() {
    __doPostBack('ctl00$MainContent$LinkButton1', '');
}